package com.example.demo.sensitive.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.demo.sensitive.dto.request.DatabaseConfigRequest;
import com.example.demo.sensitive.dto.response.DatabaseConfigResponse;
import com.example.demo.sensitive.entity.DatabaseConfig;
import lombok.Data;

import java.util.List;

/**
 * @ClassName DatabaseService
 * @Description 数据库服务接口
 * @Author System
 * @Date 2025/9/19
 **/
public interface DatabaseService extends IService<DatabaseConfig> {

    /**
     * 分页查询数据库配置
     */
    Page<DatabaseConfigResponse> getPageList(int current, int size, Integer status, String dbType, String keyword);

    /**
     * 根据ID查询数据库配置
     */
    DatabaseConfigResponse getDatabaseConfigById(Long id);

    /**
     * 创建数据库配置
     */
    Long create(DatabaseConfigRequest request);

    /**
     * 更新数据库配置
     */
    void update(Long id, DatabaseConfigRequest request);

    /**
     * 删除数据库配置
     */
    void delete(Long id);

    /**
     * 启用/禁用数据库配置
     */
    void updateStatus(Long id, Integer status);

    /**
     * 获取所有启用的数据库配置
     */
    List<DatabaseConfigResponse> getEnabledConfigs();

    /**
     * 测试数据库连接
     */
    ConnectionTestResult testConnection(Long id);

    /**
     * 测试数据库连接使用配置)
     */
    ConnectionTestResult testConnection(DatabaseConfigRequest request);

    /**
     * 获取数据库表列表
     */
    List<TableInfo> getTables(Long databaseId);

    /**
     * 获取表结构信息
     */
    TableStructure getTableStructure(Long databaseId, String tableName);

    /**
     * 获取数据库统计信息
     */
    List<DbTypeStatistics> getDbTypeStatistics();

    /**
     * 连接测试结果内部类
     */
    @Data
    class ConnectionTestResult {
        private Boolean connected;
        private Long latency;
        private String version;
        private Integer tableCount;
        private String errorMessage;
    }

    /**
     * 表信息内部类
     */
    @Data
    class TableInfo {
        private String tableName;
        private String tableComment;
        private Long rowCount;
        private String engine;
        private String charset;
        private String createTime;
        private String updateTime;
        private Long dataLength;
        private Long indexLength;
    }

    /**
     * 表结构内部类
     */
    @Data
    class TableStructure {
        private String tableName;
        private String tableComment;
        private List<ColumnInfo> columns;
    }

    /**
     * 列信息内部类
     */
    @Data
    class ColumnInfo {
        private String columnName;
        private String dataType;
        private String columnComment;
        private Boolean nullable;
        private String defaultValue;
        private Boolean isPrimaryKey;
    }

    /**
     * 数据库类型统计内部类
     */
    @Data
    class DbTypeStatistics {
        private String dbType;
        private Integer count;
        private Integer enabledCount;
    }

    /**
     * 连接信息内部类
     */
    @Data
    class ConnectionInfo {
        private String version;
        private String currentDatabase;
        private String currentTime;
    }
}
